package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeSectionReportService;
import com.bidevalution.dao.entity.*;
import com.bidevalution.dao.mapper.*;
import com.bidevalution.service.BidResultService;
import com.bstek.ureport.Utils;
import com.bstek.ureport.export.ExportConfigure;
import com.bstek.ureport.export.ExportConfigureImpl;
import com.bstek.ureport.export.ExportManager;
import com.bstek.ureport.model.Report;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.qtp.common.constants.SystemConstants;
import com.qtp.core.config.PropertiesTookit;
import com.qtp.core.model.Response;
import com.qtp.core.util.FileUtils;
import com.qtp.core.util.OConvertUtils;
import com.qtp.core.util.Ureport2Util;
import com.qtp.core.util.UreportExcel;
import org.apache.log4j.Logger;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 查慧英
 * @date 2019-11-12 10:07
 */

/**质询表可在评标过程中查看、暗标关联表可在整个技术标评审结束后查看，其他表要求在评审过程中可根据每个环节进行数据的加载，如解决此问题，则同时取消评审结束打印报表的限制（报表打印控制取消，
 * 技术标环节控制不能查看暗标关联表，技术标评审完成后在下一环节，组长可以打印暗标关联表）
 * ZTKP-3572
 * 查慧英
 * 2020-03-23
 */
@Service(value = "beSectionReportService")
public class BeSectionReportServiceImpl implements BeSectionReportService {
    private Logger logger = Logger.getLogger(getClass());
    private static final String REPORT_PATHS = "reportPaths";
    private static final String COMMA = ",";
    private static final String PATH_FORMAT="%s%s.%s";
    private static final String NAME_FORMAT="%s%s";
    private static final String PDF_SUFFIX = "pdf";
    private static final String SECTION_ID = "sectionId";
    private static final String STEP_CODE = "stepCode";
    private static final String REPORT_EXPERT = "reportExpert";
    private static final String EXPERT_ID = "expertId";
    private static final String SECTION_CODE = "sectionCode";
    private static final String TEMP_REPORT = "PublicContentTempPdf";
    private static int interval = 400;
    private static final String EXCEL_SUFFIX = PropertiesTookit.getValue("ureport.excel.suffix");
    private static final String EXPORT_PATH = PropertiesTookit.getValue("ureport.export.path");
    @Autowired(required = false)
    BeSectionReportMapper beSectionReportMapper;
    @Autowired(required = false)
    private BeSectionExpertInfoMapper beSectionExpertInfoMapper;
    @Autowired(required = false)
    BidResultService bidResultService;
    @Autowired(required = false)
    BeSectionParameterMapper beSectionParameterMapper;
    @Autowired(required = false)
    BeSectionInfoMapper beSectionInfoMapper;
    @Autowired(required = false)
    BeSysParamsMapper beSysParamsMapper;
    @Override
    public Response<List<BeSectionReport>> getBeSectionReportList(Map<String, Object> parameters) {

        BeSectionExpertInfo expertInfo = getExpertInfo(parameters);
        if(expertInfo != null) {
            if (!SystemConstants.STR_1.equals(OConvertUtils.getString(expertInfo.getIsManager()))) {
                parameters.put(REPORT_EXPERT, SystemConstants.NUM_1);
            }
        }
        String sectionId = OConvertUtils.getString(parameters.get(SECTION_ID));
        String[] paramIds = {"Module","Defence","GuideType"};
        List<BeSectionParameter> parmList = beSectionParameterMapper.queryListByParamIds(sectionId, paramIds);
        if (parmList != null && !parmList.isEmpty()) {
            for (BeSectionParameter item : parmList) {
                if ("Defence".equals(item.getParamId())) {
                    if ("0".equals(item.getParamValue())) {
                        parameters.put("notDefence", "投标人陈述和答辩评审情况表");
                    }
                } else if ("Module".equals(item.getParamId())) {
                    if ((OConvertUtils.getInt(item.getParamValue()) & 4) != 4) {
                        parameters.put("module", "资信标评审%");
                    }
                } else if ("GuideType".equals(item.getParamId())) {
                    if (!"2".equals(item.getParamValue())) {
                        parameters.put("GuideType", "投标文件定量评审情况表");
                        parameters.put("GuideType2", "评标委员会独立投票意见表%");
                    }
                }
            }
        }
        //begin
        //质询表可在评标过程中查看、暗标关联表可在整个技术标评审结束后查看，其他表要求在评审过程中可根据每个环节进行数据的加载，如解决此问题，则同时取消评审结束打印报表的限制（报表打印控制取消，
        // * 技术标环节控制不能查看暗标关联表，技术标评审完成后在下一环节，组长可以打印暗标关联表）
        // * ZTKP-3572
        // * 查慧英
        // * 2020-03-23
        BeSectionInfo beSectionInfo = beSectionInfoMapper.getSectionInfoById(sectionId);
        if (OConvertUtils.getInt(beSectionInfo.getReviewState()) < 3) {
            List<BeSysParams> list = beSysParamsMapper.selectForList("ABDBGLBBKG");
            if (list != null && !list.isEmpty()) {
                BeSysParams sysParams = list.get(0);
                if (OConvertUtils.getInt(sysParams.getSypaValue()) == 0) {
                    parameters.put("darkMark", 0);
                }
            }
        }
        //end
        return Response.ok(beSectionReportMapper.list(parameters));
    }
    @Override
    public Response exportPdf(Map<String, Object> parameters, String exportPath) {
        OutputStream os = null;
        OutputStream osTemp = null;
        String sectionId = OConvertUtils.getString(parameters.get(SECTION_ID));
        try {
            BeSectionExpertInfo expertInfo = getExpertInfo(parameters);
            parameters.put(EXPERT_ID, expertInfo.getSeqId());
            Response<List<BeSectionReport>> response = getBeSectionReportList(parameters);
            if (response.isSuccess()) {
                List<BeSectionReport> list = response.getResult();
                if (list != null && !list.isEmpty()) {
                    String reportName;
                    String pdfPath;
                    String paths;
                    String pdfPathTemp;
                    Document document = null;
                    for (BeSectionReport item : list) {
                        reportName = item.getReportName();
                        parameters.put(STEP_CODE, item.getSectionStepCode());
                        paths = item.getReportPath();
                        parameters.put(REPORT_PATHS, paths);
                        pdfPath = String.format(PATH_FORMAT, exportPath, reportName, PDF_SUFFIX);
                        String[] pathArr = paths.split(COMMA);
                        os = new FileOutputStream(new File(pdfPath));
                        if (pathArr.length > SystemConstants.NUM_1) {
                            //获取水印码
                            String watermark = getWatermark(sectionId);
                            pdfPathTemp = String.format(PATH_FORMAT, exportPath, TEMP_REPORT, PDF_SUFFIX);
                            osTemp = new FileOutputStream(new File(pdfPathTemp));
                            List<String> fileList = getFileList(parameters, pathArr, exportPath, reportName);
                            morePdfToPdf(fileList, osTemp);
                            if (OConvertUtils.isNotEmpty(watermark)) {
                                PdfReader reader = new PdfReader(pdfPathTemp);
                                waterMark(reader, os, watermark);
                            }
                            FileUtils.deleteFiles(pdfPathTemp);
                        } else {
                            document = producePdf(document, os, parameters, paths, false);
                            document.close();
                        }
                    }
                }
            }
            return Response.ok();
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            return Response.fail(e.getMessage());
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
            if (osTemp != null) {
                try {
                    osTemp.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }

    @Override
    public Response exportExcel(Map<String, Object> parameters, String exportPath) {
        SXSSFWorkbook wb = null;
        try {
            BeSectionExpertInfo expertInfo = getExpertInfo(parameters);
            parameters.put(EXPERT_ID, expertInfo.getSeqId());
            UreportExcel excel = new UreportExcel();
            Response<List<BeSectionReport>> response = getBeSectionReportList(parameters);
            if (response.isSuccess()) {
                List<BeSectionReport> list = response.getResult();
                if (list != null && !list.isEmpty()) {
                    Report report;
                    OutputStream os;
                    String reportName;
                    String reportPath;
                    String[] pathArr;
                    String excelPath;
                    for (BeSectionReport item : list) {
                        reportName = item.getReportName();
                        reportPath = item.getReportPath();
                        parameters.put(REPORT_PATHS, reportPath);
                        parameters.put(STEP_CODE, item.getSectionStepCode());
                        excelPath = String.format(PATH_FORMAT, exportPath, reportName, EXCEL_SUFFIX);
                        if (OConvertUtils.isNotEmpty(reportPath)) {
                            wb = new SXSSFWorkbook(100000);
                            os = new FileOutputStream(new File(excelPath));
                            pathArr = reportPath.split(COMMA);
                            for (int i = 0; i < pathArr.length; i++) {
                                report = Ureport2Util.getReport(pathArr[i], parameters);
                                excel.build(wb, report, null);
                            }
                            wb.write(os);
                        }
                    }
                }
            }
            return Response.ok();
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        }finally{
            if(wb != null) {
                wb.dispose();
            }
        }
    }
    @Override
    public Response exportExcelSheet(Map<String, Object> parameters, String exportPath) {
        SXSSFWorkbook wb = new SXSSFWorkbook(100000);
        String sectionId = OConvertUtils.getString(parameters.get(SECTION_ID));
        OutputStream os = null;
        try {
            BeSectionExpertInfo expertInfo = getExpertInfo(parameters);
            parameters.put(EXPERT_ID, expertInfo.getSeqId());
            UreportExcel excel = new UreportExcel();
            Response<List<BeSectionReport>> response = getBeSectionReportList(parameters);
            if (response.isSuccess()) {
                List<BeSectionReport> list = response.getResult();
                if (list != null && !list.isEmpty()) {
                    Report report;
                    String excelPath = String.format(PATH_FORMAT, exportPath, sectionId, EXCEL_SUFFIX);
                    os = new FileOutputStream(new File(excelPath));
                    String reportName;
                    String name;
                    String reportPath;
                    String[] pathArr;
                    for (BeSectionReport item : list) {
                        reportName = item.getReportName();
                        reportPath = item.getReportPath();
                        parameters.put(REPORT_PATHS, reportPath);
                        parameters.put(STEP_CODE, item.getSectionStepCode());
                        if (OConvertUtils.isNotEmpty(reportPath)) {
                            pathArr = reportPath.split(COMMA);
                            for (int i = 0; i < pathArr.length; i++) {
                                name = reportName;
                                if (i > 0) {
                                    name = String.format(NAME_FORMAT,reportName,i);
                                }
                                report = Ureport2Util.getReport(pathArr[i], parameters);
                                excel.build(wb, report, name);
                            }
                        }
                    }
                    wb.write(os);
                }
            }
            return Response.ok();
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        }finally{
            if(os != null) {
                try {
                    os.close();
                }catch (Exception ex) {
                    logger.error(ex.getMessage(), ex);
                }
            }
            wb.dispose();
        }
    }

    @Override
    public void batchPrintPdf(Map<String, Object> parameters, OutputStream os) {
        Document document = null;
        OutputStream outputStream = null;
        try {
            BeSectionExpertInfo expertInfo = getExpertInfo(parameters);
            parameters.put(EXPERT_ID, expertInfo.getSeqId());
            Response<List<BeSectionReport>> response = getBeSectionReportList(parameters);
            if (response.isSuccess()) {
                String paths;
                List<BeSectionReport> list = response.getResult();
                String pdfPath;
                if (list != null && !list.isEmpty()) {
                    List<String> fileList = new ArrayList<>();
                    for (BeSectionReport item : list) {
                        parameters.put(STEP_CODE, item.getSectionStepCode());
                        paths = item.getReportPath();
                        parameters.put(REPORT_PATHS, paths);
                        pdfPath = String.format(PATH_FORMAT, EXPORT_PATH, item.getReportName(), PDF_SUFFIX);
                        outputStream = new FileOutputStream(new File(pdfPath));
                        document = producePdf(document, outputStream, parameters, paths, false);
                        document.close();
                        outputStream.flush();
                        outputStream.close();
                        fileList.add(pdfPath);
                    }
                    morePdfToPdf(fileList, os);
                    os.close();
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
            if (document != null) {
                document.close();
            }
        }
    }

    @Override
    public Response pushPdf(Map<String, Object> parameters, String exportPath) {
        String sectionCode = OConvertUtils.getString(parameters.get(SECTION_CODE));
        String sectionId = OConvertUtils.getString(parameters.get(SECTION_ID));
        String paths = getPushPaths(sectionId);
        try {
            //获取水印码
            String watermark = getWatermark(sectionId);
            String[] pathArr = paths.split(COMMA);
            List<String> fileList = getPushList(parameters, pathArr, exportPath, sectionCode);
            exportPath = String.format(PATH_FORMAT, exportPath, sectionCode, PDF_SUFFIX);
            String pdfPathTemp = String.format(PATH_FORMAT, exportPath, TEMP_REPORT, PDF_SUFFIX);
            OutputStream osTemp = new FileOutputStream(pdfPathTemp);
            morePdfToPdf(fileList, osTemp);

            if (OConvertUtils.isNotEmpty(watermark)) {
                PdfReader reader = new PdfReader(pdfPathTemp);
                OutputStream os = new FileOutputStream(exportPath);
                waterMark(reader, os, watermark);
                os.close();
            }
            FileUtils.deleteFiles(pdfPathTemp);
            byte[] fileByte = fileToByte(exportPath);

            return bidResultService.pushPdf(sectionCode, fileByte);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);

            return Response.fail(e.getMessage());
        }
    }

    @Override
    public void updateReportExpert(Map<String, Object> params) {
        beSectionReportMapper.updateReportExpert(params);
    }

    private String getGsPaths(String sectionId) {
        return beSectionReportMapper.getGsPaths(sectionId);
    }
    private String getPushPaths(String sectionId) {
        return beSectionReportMapper.getPushPaths(sectionId);
    }

    @Override
    public void printPdf(Map<String, Object> parameters, OutputStream os) {
        Document document = null;
        try {
            BeSectionExpertInfo expertInfo = getExpertInfo(parameters);
            if(expertInfo!=null) {
                parameters.put(EXPERT_ID, expertInfo.getSeqId());
            }
            String paths = OConvertUtils.getString(parameters.get(REPORT_PATHS));
            logger.info("打印报表路径："+paths);
            String sectionId = OConvertUtils.getString(parameters.get(SECTION_ID));
            if (OConvertUtils.isEmpty(paths)) {
                paths = getGsPaths(sectionId);
                logger.info("打印报表路径2："+paths);
            }

            String[] pathArr = paths.split(COMMA);
            if (pathArr.length > SystemConstants.NUM_1) {
                logger.info("打印报表个数："+pathArr.length);
                //获取水印码
                String watermark = getWatermark(sectionId);
                String pdfPath = String.format(PATH_FORMAT, EXPORT_PATH, TEMP_REPORT, PDF_SUFFIX);
                try (OutputStream outputStream = new FileOutputStream(new File(pdfPath))){
                    List<String> fileList = getFileList(parameters, pathArr, EXPORT_PATH, TEMP_REPORT);
                    morePdfToPdf(fileList, outputStream);
                }
                if (OConvertUtils.isNotEmpty(watermark)) {
                    PdfReader reader = new PdfReader(pdfPath);
                    waterMark(reader, os, watermark);
                }
                os.flush();
                os.close();
                FileUtils.deleteFiles(pdfPath);
            } else {
                document = producePdf(document, os, parameters, paths, false);
                document.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (document != null) {
                document.close();
            }
        }

    }
    /**
     * 生成pdf
     * @param document document
     * @param os os
     * @param parameters 参数
     * @param paths 报表模板路径
     * @param flag
     * @return document
     */
    private Document producePdf(Document document, OutputStream os, Map<String, Object> parameters, String paths, Boolean flag) {
        if (OConvertUtils.isNotEmpty(paths)) {
            String[] pathArr = paths.split(COMMA);
            Report report;
            Rectangle pageSize;
            for (int i = 0; i < pathArr.length; i++) {
                report = Ureport2Util.getReport(pathArr[i], parameters);
                pageSize = Ureport2Util.getPageSize(report);
                if (i == 0 && !flag) {
                    document = Ureport2Util.getDocument(report, pageSize);
                } else {
                    document.setPageSize(pageSize);
                }
                PdfWriter writer = null;
                try {
                    writer = PdfWriter.getInstance(document, os);
                } catch (DocumentException e) {
                    logger.error(e.getMessage(), e);
                }
                Ureport2Util.produce(report, document, writer, pageSize);
            }

        }
        return document;
    }
    /**
     * 获取专家信息
     * @param parameters 参数
     * @return 专家信息
     */
    private BeSectionExpertInfo getExpertInfo(Map<String,Object> parameters) {
        String loginName = OConvertUtils.getString(parameters.get("loginName"));
        String sectionId = OConvertUtils.getString(parameters.get("sectionId"));
        return beSectionExpertInfoMapper.selectByLoginName(sectionId, loginName);
    }
    /**
     * 导出PDF并组装报表路径列表
     * @param parameters 参数
     * @param pathArr 报表模板路径
     * @param exportPath 导出路径
     * @param reportName 报表名称
     * @return 报表路径列表
     */
    private List<String> getPushList(Map<String, Object> parameters, String[] pathArr, String exportPath, String reportName) {
        String sectionId = OConvertUtils.getString(parameters.get(SECTION_ID));
        ExportManager exportManager = (ExportManager) Utils.getApplicationContext().getBean(ExportManager.BEAN_ID);
        List<String> fileList = new ArrayList<>();
        String filePath;
        String unitName;
        try {
            List<Map<String, Object>> unitList = beSectionReportMapper.getBidUnit(sectionId);
            for (int i = 0; i < pathArr.length; i++) {
                if (i==1) {
                    if (unitList != null && !unitList.isEmpty()) {
                        for (int j = 0; j < unitList.size(); j++) {
                            unitName = OConvertUtils.getString(unitList.get(j).get("DWMC"));
                            parameters.put("unitName", unitName);
                            filePath = String.format(PATH_FORMAT, exportPath, pathArr[i], reportName+i+j , PDF_SUFFIX);
                            fileList.add(filePath);
                            pushReport(exportManager, parameters, pathArr[i], filePath);
                        }
                    }
                } else {
                    filePath = String.format(PATH_FORMAT, exportPath, pathArr[i], reportName+i , PDF_SUFFIX);
                    fileList.add(filePath);
                    pushReport(exportManager, parameters, pathArr[i], filePath);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return fileList;
    }

    private void pushReport(ExportManager exportManager, Map<String, Object> parameters, String path, String filePath) {
        OutputStream os = null;
        try {
            os = new FileOutputStream(new File(filePath));
            ExportConfigure exportConfigure = new ExportConfigureImpl(path, parameters, os);
            exportManager.exportPdf(exportConfigure);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }

    }

    /**
     * 导出PDF并组装报表路径列表
     * @param parameters 参数
     * @param pathArr 报表模板路径
     * @param exportPath 导出路径
     * @param reportName 报表名称
     * @return 报表路径列表
     */
    private List<String> getFileList(Map<String, Object> parameters, String[] pathArr, String exportPath, String reportName) {
        ExportManager exportManager = (ExportManager) Utils.getApplicationContext().getBean(ExportManager.BEAN_ID);
        List<String> fileList = new ArrayList<>();
        ExportConfigure exportConfigure;
        OutputStream os;
        String filePath;
        try {
            for (int i = 0; i < pathArr.length; i++) {
                filePath = String.format(PATH_FORMAT, exportPath, reportName+i , PDF_SUFFIX);
                os = new FileOutputStream(new File(filePath));
                exportConfigure = new ExportConfigureImpl(pathArr[i], parameters, os);
                exportManager.exportPdf(exportConfigure);
                os.close();
                fileList.add(filePath);
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return fileList;
    }

    /**
     * 获取水印码
     * @param sectionId 标段id
     * @return 水印码
     */
    private String getWatermark(String sectionId) {
        return beSectionReportMapper.getWatermark(sectionId);
    }
    /**
     * 合并报表
     * @param fileList 报表路径列表
     * @param os 合并的文件流
     */
    private void morePdfToPdf(List<String> fileList, OutputStream os) {
        Document document = null;
        PdfReader reader = null;
        try {
            document = new Document();
            PdfCopy copy = new PdfCopy(document, os);
            document.open();
            PdfImportedPage page;
            for (int i = 0; i < fileList.size(); i++) {
                reader = new PdfReader(fileList.get(i));
                // 获得总页码
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    // 从当前Pdf,获取第j页
                    page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                    page.closePath();
                }

                reader.close();
                FileUtils.deleteFiles(fileList.get(i));
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } catch (DocumentException e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (document != null) {
                document.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }
    //给PDF添加水印
    //inputFile 文件路径+名称
    //outputFile 添加水印后输出文件保存的路径+名称
    //waterMarkName 添加水印的内容
    public void waterMark(PdfReader reader, OutputStream os, String waterMarkName) {
        try {
            PdfStamper stamper = new PdfStamper(reader, os);

            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.3f);
            gs.setStrokeOpacity(0.4f);
            int total = reader.getNumberOfPages() + 1;

            JLabel label = new JLabel();
            FontMetrics metrics;
            int textH = 0;
            int textW = 0;
            label.setText(waterMarkName);
            metrics = label.getFontMetrics(label.getFont());
            textH = metrics.getHeight();
            textW = metrics.stringWidth(label.getText());

            PdfContentByte under;
            for (int i = 1; i < total; i++) {
                under = stamper.getOverContent(i);
                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base, 20);

                // 水印文字成30度角倾斜
                int height = interval + textH;
                int width = interval - textW;
                under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height + textH, 30);
                // 添加水印文字
                under.endText();
            }
            stamper.close();
            reader.close();
            os.flush();
            os.close();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    /**
     * 生成报表二进制流
     * @param filePath 报表路径
     * @return 报表二进制流
     */
    private byte[] fileToByte(String filePath)
    {
        FileInputStream fileInputStream = null;
        File file = new File(filePath);
        byte[] bFile = new byte[(int) file.length()];
        try {
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            return bFile;
        }catch(Exception e){
            logger.error(e.getMessage(), e);
        }finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                }catch (Exception ex){
                    logger.error(ex.getMessage(), ex);
                }
            }
        }
        return null;
    }
}
